package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Clazz;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.School;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Speciality;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.University;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IClazzService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ISpecialityService;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 金雪松
 */
@Controller
@RequestMapping("/Clazz")
public class ClazzController {
    @Autowired
    private IClazzService clazzService;
    @Resource
    private ISpecialityService specialityService;

    @RequestMapping(value = {"/toAddClazz"})
    public String toAddClazz(Model model) {
        List<Speciality> specialities = specialityService.queryAllSpeciality();
        model.addAttribute("specialities", specialities);
        return "toaddclazz";
    }

    @RequestMapping(value = {"/toUpdateClazz"})
    public ModelAndView toUpdateClazz(Integer id) {
        Clazz clazz = clazzService.queryClazzById(id);
        ModelAndView model = new ModelAndView("toupdateclazz");

        List<Speciality> specialities = specialityService.queryAllSpeciality();
        model.addObject("specialities", specialities);
        model.addObject("clazz", clazz);
        return model;
    }

    @RequestMapping("insertClazz")
    @ResponseBody
    public String insertClazz(Integer id,String name,String grade,Integer specialityId,Integer number,String note)
    {
        Clazz clazz = new Clazz(id,name,grade,specialityId,number,note);
        clazzService.insertClazz(clazz);
        return "OK";
    }

    @RequestMapping("queryAllClazz")
    public String queryAllClazz(Model model,@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
                                @RequestParam(value = "pageSize",defaultValue = "5") int pageSize)
    {
        int totalCount = clazzService.queryTotalCount();
        int pageCount = (int)Math.ceil((double) totalCount / pageSize);
        List<Map<String,Object>> clazzes = new ArrayList<Map<String,Object>>();
        List<Clazz> clazzes_ = clazzService.queryClazzByPage(pageNow,pageSize);

        if(clazzes_ != null && clazzes_.size() > 0){
            for (Clazz clazz_ : clazzes_) {
                Map<String,Object>  clazz = new HashMap<>();
                Speciality speciality = specialityService.querySpecialityById(clazz_.getSpecialityId());
                clazz.put("id",clazz_.getId());
                clazz.put("name",clazz_.getName());
                clazz.put("grade",clazz_.getGrade());
                clazz.put("speciality",speciality.getName());
                clazz.put("number",clazz_.getNumber());
                clazz.put("note",clazz_.getNote());
                clazzes.add(clazz);
            }
        }
        model.addAttribute("clazzes", clazzes);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageCount",pageCount);
        return "clazz";
    }

    @ResponseBody
    @GetMapping("queryClazzById")
    public Clazz queryClazzById(Integer id)
    {
        return clazzService.queryClazzById(id);
    }

    @ResponseBody
    @RequestMapping("deleteClazzById")
    public String deleteClazzById(Integer id)
    {
        clazzService.deleteClazzById(id);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateClazzById")
    public String UpdateClazzById(Integer id,String name,String grade,Integer specialityId,Integer number,String note)
    {
        Clazz clazz = new Clazz(id,name,grade,specialityId,number,note);
        clazzService.updateClazzById(clazz);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("queryClazzByName")
    public List<Clazz> queryClazzByName(String name)
    {
        return clazzService.queryClazzByName(name);
    }
}
